﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once

#include "NdbObjectSchematic.h"
#include "IExternalData.h"
#include "IStructureBasicBeam.h"
#include "StructureBeam.h"
#include "DbDataSchema.h"
#include "RegenDataSchema.h"
#include "StructureInstanceEndType.h"

namespace gcmp
{
    class IParameter;

    class StructureBasicBeam : public NdbObjectSchematic, public IExternalData, public IStructureBasicBeam
    {
        DBOBJECT_DATA_BEGIN_AND_QUICK_CAST_FROM(gcmp, StructureBasicBeam, gcmp::NdbObjectSchematic, A8CA257F - 192B - 4825 - A845 - 798B6661F25F, gmstructure, IExternalData)
            DATA(OwnerPtr<StructureBeam>, BaseData)
            ENUM_DATA_REGEN(InstanceEndType, StartEndFaceSectionType) // 起始面、终止面截面样式
            DATA_REGEN(double, Angle) // 倾斜角度
            DATA_REGEN(double, BottomElevation)
            DATA_REGEN(double, StartTopElevation)
            DATA_REGEN(double, EndTopElevation)
        DBOBJECT_DATA_END

    public:
        static StructureBasicBeam* Create(const StructureBeamInput* pInput);

        static StructureBasicBeam* Get(IInstance* pInstance);
        static const StructureBasicBeam* Get(const IInstance* pInstance);

    private:
        static IInstance* CreateStraightBeam(const StructureBeamInput* pInput);
        static IInstance* CreateArcBy3PtsBeam(const StructureBeamInput* pInput);

        static StructureBasicBeam* RegisterExternalData(IInstance* pInstance, const StructureBeamInput* pInput);

    public:
        // 继承自IStructureBasicBeam
        DECLARE_STRUCTURE_INSTANCE_METHODS;

        virtual BeamZPositioningType GetZPositioningType() const override;
        virtual bool SetZPositioningType(BeamZPositioningType type) override;

        virtual InstanceEndType GetStartEndFaceSectionType() const override { return GetStartEndFaceSectionType__(); }
        bool SetStartEndFaceSectionType(InstanceEndType type) { SetStartEndFaceSectionType__(type); return true; }

        virtual StructureInstanceSectionShapeType GetSectionShapeType() const override;

        virtual double GetVolume() const override;
        virtual double GetAngle() const override { return GetAngle__(); }
        virtual double GetLength() const override;
        virtual bool Transform(const Matrix4d& matrix) override { return true; }
    private:
        bool SetVolume(double volume);
        bool SetLength(double length);
        bool SetAngle(double angle) { SetAngle__(angle); return true; }

        //高程信息
    private:
        bool SetTopElevation(double value) { return GetBaseDataFW__()->SetTopElevation(value); }
        bool SetBottomElevation(double value) { SetBottomElevation__(value); return true; }
        bool SetStartTopElevation(double value) { SetStartTopElevation__(value); return true; }
        bool SetStartBottomElevation(double value) { return GetBaseDataFW__()->SetStartBottomElevation(value); }
        bool SetEndTopElevation(double value) { SetEndTopElevation__(value); return true; }
        bool SetEndBottomElevation(double value) { return GetBaseDataFW__()->SetEndBottomElevation(value); }
    public:
        double GetTopElevation() const { return  GetBaseData__()->GetTopElevation(); }
        double GetBottomElevation() const { return GetBottomElevation__(); }
        double GetStartTopElevation() const { return GetStartTopElevation__(); }
        double GetStartBottomElevation() const { return  GetBaseData__()->GetStartBottomElevation(); }
        double GetEndTopElevation() const { return GetEndTopElevation__(); }
        double GetEndBottomElevation() const { return  GetBaseData__()->GetEndBottomElevation(); }

    private:
        virtual bool SetOwnerElement(IElement* pOwnerElement) override;
        virtual IElement* GetOwnerElement() override;
        virtual const IElement* GetOwnerElement() const override;
        IDocument* GetDocument() const;
        ElementId GetOwnerElementId() const;
        virtual NdbObject* GetTopOwnerObject() const override;

    private:
        virtual void ReportElementIds(std::set<ElementId>* pElementIds, std::unordered_map<ElementId, std::vector<std::wstring>, ElementIdHash>* pPaths,
            std::vector<std::wstring>* pCurrentPath) const final {}
        virtual void ReplaceElementIds(const std::unordered_map<ElementId, ElementId, ElementIdHash>& oldToNewMap) final {}

        virtual void UpdateForWeakParentDeletion(const std::set<ElementId>& deletedElementIds) override;
        virtual void ReportParents(IElementParentReporter& reporter) const override;
        virtual void GetCalculators(ICalculatorCollection* calculators) const override;

        virtual void ReportParameterDefinitions(std::vector<int>* pParamDefIds) const override;
        virtual OwnerPtr<IParameter> GetNativeParameter(int paramDefId) const override;
        virtual bool SetNativeParameter(const IParameter *param, std::wstring* errorMsg = nullptr) override;
        virtual bool IsParameterModifiable(int paramDefId) const override;

        virtual void Report(const gcmp::ICopyContext & copyContext, gcmp::ICopyStrategyReporter& reporter) const override;
        virtual void UpdateAfterCopy(const ICopyContext& copyContext) override {};
        virtual void UpdateForWeakParentCopied(const IDocument* pDocumentFrom, const IElement* pSource, const IElement* pCopy) const override {};
        virtual bool ShouldBeCopiedTogether(const ICopyContext & copyContext) const override { return true; };
        virtual bool IsStrongParentsNeedCopied(void) const override { return true; };
        virtual bool CanBeCopied(std::wstring* pErrorMessage) const override { return true; };

    };
}
